<?php

namespace app\stores\controller\miniapp\withdraw;

use app\BaseController;
use app\common\model\DataConfigModel;
use app\stores\model\promotion\PromotionrecordModel;
use app\stores\model\withdraw\MemberWithdrawAccountModel;
use think\facade\Db;

class Index extends BaseController{
    /*获取提现账户*/
    public function withdrawAccount(){
        $type = $this->request->type;
        $team_id = $this->request->param('team_id/d',0);

        $member_id = $type==2?0:$this->request->memberId;

        if($type==2) $where[] = ['team_id','=',$team_id];
        $where[] = ['member_id','=',$member_id];
        $where[] = ['is_delete','=',0];
        $withdraw_account = MemberWithdrawAccountModel::info($where);
        //用户提现手续费
        $dataconfig=new DataConfigModel();
        $info=$dataconfig->where(['id'=>1])->find();
        $withdraw_account['user_money_ratio'] = $info['user_money_ratio'];
        //获取余额
        $record_where[] = ['team_id','=',$team_id];
        $record_where[] = ['member_id','=',$member_id];
        $record = PromotionrecordModel::info($record_where);
        $withdraw_account['member_money'] = sprintf("%.2f",empty($record)?0:$record['money']);
        return $this->success($withdraw_account);

    }
    /*提现账户新增/修改*/
    public function saveWithdrawAccount(){
        $type = $this->request->type;
        $team_id = $this->request->param('team_id/d',0);
        $account_type = $this->request->param('account_type/d',1);//1银行账户2支付宝账户
        $open_bank_name = $this->request->param('open_bank_name');
        $open_branch = $this->request->param('open_branch');
        $account_name = $this->request->param('account_name');
        $account_number = $this->request->param('account_number');

        if($account_type ==1 && empty($open_bank_name)) return $this->error('开户行不能为空');
        if($account_type ==1 && empty($open_branch)) return $this->error('开户支行不能为空');
        if(empty($account_name)) return $this->error('户名不能为空');
        if(empty($account_number)) return $this->error('账号不能为空');

        $member_id = $type==2?0:$this->request->memberId;
        if($type==2) $where[] = ['team_id','=',$team_id];
        $where[] = ['member_id','=',$member_id];
        $where[] = ['is_delete','=',0];
        if($withdraw_account_info = MemberWithdrawAccountModel::info($where)){
            return $this->error('提现账户已存在');
        }
        $withdraw_account = new MemberWithdrawAccountModel();
        $withdraw_account->team_id = $team_id;
        $withdraw_account->member_id = $type==2?0:$member_id;
        $withdraw_account->open_bank_name = $open_bank_name;
        $withdraw_account->open_branch = $open_branch;
        $withdraw_account->account_name = $account_name;
        $withdraw_account->account_number = $account_number;
        $withdraw_account->type = $account_type;
        $withdraw_account->save();
        return $this->success('保存成功');
    }
    /*删除提现账户*/
    public function delWithdrawAccount(){
        $type = $this->request->type;
        $team_id = $this->request->param('team_id/d',0);

        $member_id = $type==2?0:$this->request->memberId;

        if($type==2) $where[] = ['team_id','=',$team_id];

        $where[] = ['member_id','=',$member_id];

        if($where){

            MemberWithdrawAccountModel::up(['is_delete'=>1],$where);
        }

        return $this->success('删除成功');
    }

}

